package shopping;

import java.util.Scanner;

public class ShoppingCart {

    public static class Goods {
        private String goodName;
        private int count = 0;

        public Goods() {}
        public Goods(String goodName, int count) {
            this.goodName = goodName;
            this.count = count;
        }
        public void setGoodName(String goodName) {
            this.goodName = goodName;
        }
        public String getGoodName() {
            return goodName;
        }
        public void setCount(int count) {
            this.count = count;
        }
        public int getCount() {
            return count;
        }
    }
    public static class Count {
        private int count=0;
        public Count() {}
        public Count(int count) {
            this.count = count;
        }
        public void setCount(int count) {
            this.count = count;
        }
        public int getCount() {
            return count;
        }
        public int addCount(int count) {
            if(this.count + count<=100){
                this.count += count;
                return 1;
            } else {
                System.out.println("购买数量超过了购物车装载极限");
                return 0;
            }
        }
    }

    private int GoodsKind=0;
    private Goods[] goods = new Goods[20];
    private Count shoppingCount = new Count();

    public ShoppingCart() {}
    public ShoppingCart(int GoodsKind, Goods[] goods, Count shoppingCount) {
        this.GoodsKind = GoodsKind;
        this.goods = goods;
        this.shoppingCount = shoppingCount;
    }

    public void setShoppingCount(Count shoppingCount) {
        this.shoppingCount = shoppingCount;
    }
    public Count getShoppingCount() {
        return shoppingCount;
    }
    public void setGoods(Goods[] goods) {
        this.goods = goods;
    }
    public Goods[] getGoods() {
        return goods;
    }
    public void setGoodsKind(int GoodsKind) {
        this.GoodsKind = GoodsKind;
    }
    public int getGoodsKind() {
        return GoodsKind;
    }

    public int ShoppingBuy() {
        String goodsname = null;
        int count=0, res=0,buyflag=0;
        while (buyflag==0) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请选择要购买的商品");
            goodsname = scanner.next();
            if(goodsname.equals("\"原神,启动\""))
            {
                System.out.println("鉴定你为O批,取消你的购物资格");
                return 1;
            }
            for (Project project : Main.projects) {
                if (project.getName().equals(goodsname)) {
                    while(true) {
                        System.out.println("它的商品种类是" + project.getKind() + "\n它的库存量有" + project.getCount() + "件\n他的价格是" + project.getPrice() + "元每件");
                        System.out.println("请输入要购买的数量\n(购物车最多装载100件商品)");
                        count = scanner.nextInt();
                        if (count > 0 && count < project.getCount()) {
                            buyflag = 1;
                            break;
                        } else {
                            buyflag = 2;
                            System.out.println("非法数量数据,请重新输入");
                        }
                    }
                }
            }
            if(buyflag==0) {
                System.out.println("非法商品数据,请重新输入");
            }
        }
        res=this.shoppingCount.addCount(count);
        if(res==1) {
            for (Goods good : goods) {
                if(good!=null) {
                    if (good.getGoodName().equals(goodsname)) {
                        for (Project p : Main.projects) {
                            if (p.getName().equals(goodsname) && p.getCount() >= count) {
                                good.count += count;
                                p.setCount(p.getCount() - count);
                                System.out.println("已在购物车中添加" + count + "份" + goodsname + ".");
                                return 0;
                            }
                        }
                    }
                }
            }
            for (Project p : Main.projects) {
                if (p.getName().equals(goodsname) && p.getCount() >= count) {
                    goods[GoodsKind]=new Goods(goodsname,count);
                    GoodsKind++;
                    p.setCount(p.getCount() - count);
                    System.out.println("已在购物车中添加" + count + "份" + goodsname + ".");
                }
            }
        }
        return 0;
    }

    public void Shoppingdel() {
        System.out.println("以下为已购买商品");
        for (Goods good :goods) {
            if(good!=null) {
                System.out.println(good.getGoodName()+"-----------"+good.getCount()+"份");
            }
        }
        System.out.println("选择要删除购物车中的物品名称和数量(空格隔开)\n(购物车最多装载100件商品)");
        String goodname ;
        int count ;
        Scanner sc2 = new Scanner(System.in);
        goodname = sc2.next();
        count = sc2.nextInt();
        for (Goods good :goods){
            if(good.getGoodName().equals(goodname)){
                if(good.getCount()<=count){
                    System.out.println("要删掉数目超过购买数,默认为全部删除");
                    good.count=0;
                    for (Project p : Main.projects) {
                        if (p.getName().equals(goodname)) {
                            p.setCount(100);
                            break;
                        }
                    }
                } else {
                    good.count -= count;
                    for (Project p : Main.projects) {
                        if (p.getName().equals(goodname)) {
                            p.setCount(p.getCount() + count);
                            break;
                        }
                    }
                    System.out.println("已删除"+count+"份"+goodname+".");
                }
                break;
            }
            System.out.println("输入的商品名称不存在,请重新选择功能");
        }
    }

    public void Shoppinglook() {
        System.out.println("以下为已购买商品\n(购物车最多装载100件商品)");
        for (Goods good :goods) {
            if(good!=null) {
                System.out.println(good.getGoodName()+"-----------"+good.getCount()+"份");
            }
        }
    }

    public int Shoppingfinish() {
        double[] sum= new double[20];
        String[] name=new String[20];
        double[] price=new double[20];
        int[] count=new int[20];
        double total = 0;
        int i=0;
        for (Goods good :goods) {
            for (Project p : Main.projects) {
                if(good!=null) {
                    if (good.goodName.equals(p.getName())) {
                        sum[i] = good.getCount() * p.getPrice();
                        name[i] = good.getGoodName();
                        price[i] = p.getPrice();
                        count[i] = good.getCount();
                        total += sum[i];
                        i++;
                        break;
                    }
                }
            }
        }
        System.out.println("===========账单如下===========");
        System.out.println("商品名称        单价        数量");
        for (int j = 0; j < i; j++) {
        System.out.println(name[j]+"           "+count[j]+"  \t\t  "+count[j]);
        }
        System.out.println("共消费---------"+total);
        return 1;
    }

}
